###############################################################################
# Makefile for the project AppCode
###############################################################################

## General Flags
PROJECT = AppCode
MCU = atmega16
TARGET = AppCode.elf
CC = avr-gcc

CPP = avr-g++

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
CFLAGS += -Wall -gdwarf-2  -Wl,--defsym=__stack=xOS+xOSstackShift  --param inline-call-cost=2  -ffunction-sections  -Wl,--gc-sections  -Wl,--relax  -Wno-main  -Winline  -Wundef   -fomit-frame-pointer  -DF_CPU=8000000UL -Os -funsigned-char -fpack-struct -fshort-enums
CFLAGS += -MD -MP -MT $(*F).o -MF dep/$(@F).d 

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS += -Wl,--defsym=__stack=xOS+xOSstackShift  --param inline-call-cost=2  -ffunction-sections  -Wl,--gc-sections  -Wl,--relax  -Wno-main  -Winline  -Wundef  -fomit-frame-pointer -Wl,-Map=AppCode.map


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings


## Include Directories
INCLUDES = -I"../../../MainCode/femtoos_headers" -I"../../../MainCode/femtoos_devices" -I"." 

## Objects that must be built in order to link
OBJECTS = femtoos_startup.o femtoos_port.o femtoos_shared.o femtoos_core.o task.o serial.o adc.o

## Objects explicitly added by the user
LINKONLYOBJECTS = 

## Build
all: $(TARGET) out/AppCode.hex out/AppCode.eep out/AppCode.lss size

## Compile
femtoos_startup.o: ../../../MainCode/femtoos_source/femtoos_startup.s
	$(CC) $(INCLUDES) $(ASMFLAGS) -c  $<

femtoos_port.o: ../../../MainCode/femtoos_source/femtoos_port.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

femtoos_shared.o: ../../../MainCode/femtoos_source/femtoos_shared.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

femtoos_core.o: ../../../MainCode/femtoos_source/femtoos_core.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

task.o: task.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

serial.o: serial.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

adc.o: adc.c
	$(CC) $(INCLUDES) $(CFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
	 $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
	avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
	-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
	avr-objdump -h -S $< > $@

size: ${TARGET}
	@echo
	@avr-size -C --mcu=${MCU} ${TARGET}

## Clean target
.PHONY: clean
clean:
	-rm -rf $(OBJECTS) AppCode.elf dep/* out/AppCode.hex out/AppCode.eep out/AppCode.lss out/AppCode.map


## Other dependencies
-include $(shell mkdir dep 2>/dev/null) $(wildcard dep/*)

